package ysq.xyg.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.StringUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import ysq.xyg.model.PersonLevel;
import ysq.xyg.model.info.Page;
import ysq.xyg.model.info.PersonLevelInfo;
import ysq.xyg.model.info.ReturnInfo;
import ysq.xyg.service.PersonLevelService;
import ysq.xyg.util.BaseConstant;
import ysq.xyg.util.BaseUtil;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;

/**
 * 分销级别
 *
 * @author congcong tao
 * @date 2018/11/27 14:22
 */
@Controller
@RequestMapping("/personLevel")
public class PersonLevelController extends BaseController {


    private static Logger log= LoggerFactory.getLogger(PersonLevelController.class);

    @Resource
    private PersonLevelService personLevelService;

    private PersonLevel personLevel;

    @RequestMapping("/listPage")
    public ModelAndView List(HttpServletRequest request, HttpServletResponse response,String menuid){
        return new ModelAndView("/personLevelList");
    }

    @RequestMapping(value = "/jsonList",method = RequestMethod.POST)
    public @ResponseBody JSONObject jsonList(PersonLevel personLevel,Page page) throws MalformedURLException, UnsupportedEncodingException {
        PageInfo<PersonLevel> pageInfo = personLevelService.pageInfo(personLevel, page.getLimit(), page.getPage());
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("code", 0);
        jsonObject.put("msg", "");
        jsonObject.put("count", pageInfo.getTotal());
        if (pageInfo != null && pageInfo.getList() != null && pageInfo.getList().size() > 0) {
            JSONObject row = null;
            JSONArray rows = new JSONArray();
            for (PersonLevel item : pageInfo.getList()) {
                row = new JSONObject();
                row.put("id", item.getId());
                row.put("name", item.getName());
                row.put("royaltyRatio", item.getRoyaltyRatio());
                row.put("levelNum", item.getLevelNum());
                row.put("fullMoney", item.getFullMoney());
                row.put("levelMoney", item.getLevelMoney());
                row.put("type", item.getType());
                rows.add(row);
            }
            jsonObject.put("data",rows);
        }
        return jsonObject;
    }

    @RequestMapping(value = "/selectEntity",method = RequestMethod.GET)
    public ModelAndView selectEntity(String id){
        ModelAndView view = new ModelAndView("/personLevelUpdateOrSave");
        personLevel = personLevelService.selectByPrimaryKey(id);
        view.addObject("personLevel", personLevel);
        return view;
    }

    @RequestMapping(value = "/updateOrAddEntity",method = RequestMethod.POST)
    public @ResponseBody JSONObject updateOrAddEntity(PersonLevelInfo personLevelInfo) {
        ReturnInfo returnInfo = new ReturnInfo();
        JSONObject jsonObject = new JSONObject();
        if (StringUtil.isEmpty(personLevelInfo.getId())) {
            personLevel = new PersonLevel();
            personLevel.setId(BaseUtil.uId());
            personLevel.setName(personLevelInfo.getName());
            personLevel.setLevelNum(personLevelInfo.getLevelNum());
            personLevel.setLevelMoney(personLevelInfo.getLevelMoney());
            personLevel.setFullMoney(personLevelInfo.getFullMoney());
            personLevel.setRoyaltyRatio(personLevelInfo.getRoyaltyRatio());
            personLevel.setType(personLevelInfo.getType());
            int id = personLevelService.insert(personLevel);
            if(id <= 0){
                returnInfo = new ReturnInfo(BaseConstant.FAIL, BaseConstant.DATABASE_ERROR_INSERT);
            }
        } else {
            personLevel = personLevelService.selectByPrimaryKey(personLevelInfo.getId());
            personLevel.setName(personLevelInfo.getName());
            personLevel.setLevelNum(personLevelInfo.getLevelNum());
            personLevel.setLevelMoney(personLevelInfo.getLevelMoney());
            personLevel.setFullMoney(personLevelInfo.getFullMoney());
            personLevel.setRoyaltyRatio(personLevelInfo.getRoyaltyRatio());
            personLevel.setType(personLevelInfo.getType());
            int id = personLevelService.updateByPrimaryKey(personLevel);
            if (id <= 0) {
                returnInfo = new ReturnInfo(BaseConstant.FAIL, BaseConstant.DATABASE_ERROR_UPDATE);
            }
        }
        jsonObject.put("returnInfo", returnInfo);
        return jsonObject;
    }

    @RequestMapping(value = "/deleteEntity",method = RequestMethod.GET)
    public @ResponseBody JSONObject deleteEntity(String id){
        ReturnInfo returnInfo=new ReturnInfo();
        JSONObject jsonObject=new JSONObject();
        if(StringUtil.isEmpty(id)){
            returnInfo=new ReturnInfo(BaseConstant.FAIL, BaseConstant.DATABASE_ERROR_CANSHU);
        }else{
            int ids = personLevelService.deleteByPrimaryKey(id);
            if(ids <= 0){
                returnInfo = new ReturnInfo(BaseConstant.FAIL, BaseConstant.DATABASE_ERROR_DELETE);
            }
        }
        jsonObject.put("returnInfo",returnInfo);
        return jsonObject;
    }
}
